<?php
// +----------------------------------------------------------------------
// | 系统框架
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 路人甲乙科技有限公司 [ http://www.lurenjiayi.com ]
// +----------------------------------------------------------------------
// | 官方网站：http://www.lurenjiayi.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\apicom\home;
use  app\member\home\Common;
use  app\money\model\Recharge as ChargeModel;
use think\Db;
/**
 * 前台首页控制器
 * @package app\money\home
 */
class Recharge extends Common
{
   
    protected function _initialize()
    {
        parent::_initialize();
        $this->debaoReturn_url = "http://www.zybok.cn/wap/member/index"; //返回商户前台同步
        $this->debaoNotice_url = "http://www.zybok.cn/index/index/payDebaoNotice";//返回商户后台异步
    }
   /**
    * 首页
    * @return [type] [description]
    */
    public function index()
    {	

    	$money = \app\money\model\Money::getMoney(MID);
        $money['account'] = bcdiv($money['account'],100,2);
        $money['operate_account'] = bcdiv($money['operate_account'],100,2);
        $money['operate_account'] = bcdiv($money['operate_account'],100,2);

		$data['money'] = $money;
		$data['account'] = config('web_site_account');
	
		ajaxmsg('用户账户信息',1,$data);

    }
	public function editCharge()
    {	

		$money = \app\money\model\Money::getMoney(MID);
    	$account=Db::name("admin_bank")->where(['status'=>1])->select();
        foreach($account as $k=>$v){
            if(!empty($v['image'])){
                $account[$k]['image'] =  get_file_path($v['image']);
            }
        }
		$data['money'] = $money;
		$data['offline'] = config('web_site_account');
		$data['account'] = $account;
        $data['kftime'] = config('web_site_service_time');
        $data['kfphone'] = config('web_site_telephone');
		
		ajaxmsg('用户账户信息',1,$data);

    }
/*
 * 充值操作
 */
	public function doCharge()
		{
		
			
			$data = $this->request->Post();
    		$line_bank = '';
            $receipt_img='';
            $charge_type_id = 0;
            $form_name = $data['form_name'];
    		$money = $data['money'];
    		if($money <= 0){
				ajaxmsg('充值金额必须大于0',0);
    		}
    		$type = $data['transfer']; // transfer 线下转账支付
    		if($type == 'transfer'){
    		    $card_id=$data['cardno'];
    			$line_bank = '卡号：'.$card_id.'；时间：'.date('Y-m-d',time());
    		}


    		$order_no = ChargeModel::saveData($money, MID, $type, $line_bank,$receipt_img,$charge_type_id,$form_name);
    		
    		if($type == 'transfer'){
                $var = Db::name('member')->where('id',MID)->value('mobile');
                $contentarr  = getconfigSms_status(['name'=>'stock_offline']);
                $content = str_replace(array("#var#","#amount#"),array($var,$money), $contentarr['value']);
                if($contentarr['status']==1){
                    sendsms_mandao('', $content, '');
                }
                ajaxmsg('充值已提交，请耐心等待审核',1);
				ajaxmsg('充值已提交，请耐心等待审核',1);
    		}else{
				ajaxmsg('走接口才能完成',0);
    		}

		}
  	//在线充值
    public function lineCharge(){
   
            $data = $this->request->post();
            $money = $data['money'];
            if($money <= 0){
                $this->error('充值金额必须大于0');
            }
            $data['mid'] = MID;
            $type = 'debao'; // transfer 线下转账支付
            $charge_type_id = $data['charge_type_id'];
            $transfer = '';
            $line_bank = '时间：'.date('Y-m-d',time());
            $order_no = ChargeModel::saveData($money, MID, $type, $line_bank,'','',$transfer);
            if($order_no){
                $data = $this->hj_pay($order_no,$money);
                 ajaxmsg('创建成功',1,$data);
            }else{
                ajaxmsg('创建订单失败',0);
            }
    }
  
    //
  	public function hj_pay($order_no,$money){
    	$post_data['mer_id'] = '1560';	//您的商户ID
        $post_data['order_id'] = $order_no;	//订单号
        $post_data['amount'] = $money;	//订单金额。单位：元
        $post_data['bank_code'] = '0002';		//银行编码
        $post_data['notify_url'] = $this->debaoNotice_url;	//异步回调通知地址

        $key = "568ff08fbf61173c0f194d076670981a";   //您的密钥
        //生成签名
        $sign=$this->encrypt_sign($post_data,$key);

        $post_data['hrefback_url'] = $this->debaoReturn_url;	//同步返回通知地址
        $post_data['attach'] = '备注信息';	//备注信息
        $post_data['sign'] = $sign;	//签名
        //p($post_data);
       	
        $post_data['api_url']='http://api.hdfpay.cn/payment/';
       	return $post_data;
    }
 	//签名算法
    public function encrypt_sign($post_data,$key){
        if(empty($post_data) || !is_array($post_data) || empty($key)){
            return false;
        }
        ksort($post_data);
        reset($post_data);
        $sign_str = "";
        foreach ($post_data as $k => $v) {
            $sign_str .= $k . "=" . $v . "&";
        }
        $sign_str .= 'key=' . $key;
        return md5($sign_str);
    }
}